package mumu.android.lib.Async;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import android.util.Log;

public class Future {
	ExecutorService executor = Executors.newSingleThreadExecutor();
	FutureTask<String> futureTask;

	public Future() {

	}

	public void call() {
		String result = "";
		// 使用Callable接口作为构造参数
		futureTask = new FutureTask<String>(new Callable<String>() {
			@Override
			public String call() throws Exception {
				Log.d("d", "run");
				Thread.sleep(3000);
				futureTask.isCancelled();
				Log.d("d", "down");
				return null;
			}
		});
		//futureTask.run();
		
		executor.execute(new Thread(futureTask));
		executor.submit(futureTask);
		// 在这里可以做别的任何事情
//		try {
//			//result = futureTask.get(5000, TimeUnit.MILLISECONDS); // 取得结果，同时设置超时执行时间为5秒。同样可以用future.get()，不设置执行超时时间取得结果
//		} catch (InterruptedException e) {
//			futureTask.cancel(true);
//		} catch (ExecutionException e) {
//			futureTask.cancel(true);
//		} catch (TimeoutException e) {
//			futureTask.cancel(true);
//		} finally {
//			executor.shutdown();
//		}
	}

	public void cancle() {
		Log.d("d", "cancle");
		futureTask.cancel(true);
	}

}
